package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.exception.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.mapper.SkuSpecificationMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.AttributeService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理属性业务的实现类
 *
 * @author Chen
 * @since 2023/2/10 18:43:43
 */
@Slf4j
@Service
public class AttributeServiceImpl implements AttributeService {

    private final AttributeMapper attributeMapper;
    private final SkuSpecificationMapper skuSpecificationMapper;

    @Autowired
    public AttributeServiceImpl(AttributeMapper attributeMapper, SkuSpecificationMapper skuSpecificationMapper) {
        log.debug("创建业务实现类对象：AttributeServiceImpl");
        this.attributeMapper = attributeMapper;
        this.skuSpecificationMapper = skuSpecificationMapper;
    }

    @Override
    public void addNew(AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理【添加属性】的业务，参数：{}", attributeAddNewDTO);
        // 检查属性名称是否已经被占用
        String name = attributeAddNewDTO.getName();
        Long templateId = attributeAddNewDTO.getTemplateId();
        int count = attributeMapper.countByNameAndTemplate(name, templateId);
        if (count >= 1) {
            String message = "添加属性失败，属性名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        // 创建Attribute对象
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO, attribute);
        log.debug("即将插入数据，参数：{}", attribute);
        int rows = attributeMapper.insert(attribute);
        if (rows != 1) {
            String message = "添加属性失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除属性】的业务，参数：{}", id);
        AttributeStandardVO attributeStandardVO = attributeMapper.getStandardById(id);
        if (attributeStandardVO == null) {
            String message = "删除属性失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        // 是否关联到其他数据
        int count = skuSpecificationMapper.countByAttributeId(id);
        if (count > 0) {
            String message = "删除属性失败，尝试删除的数据关联到其他数据！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        log.debug("即将执行删除，参数：{}", id);
        int rows = attributeMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除属性失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理【修改属性】的业务，ID：{}，参数：{}", id, attributeUpdateDTO);
        AttributeStandardVO attributeStandardVO = attributeMapper.getStandardById(id);
        if (attributeStandardVO == null) {
            String message = "修改属性失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        // 是否存在重复的属性名称
        String name = attributeUpdateDTO.getName();
        Long templateId = attributeUpdateDTO.getTemplateId();
        int count = attributeMapper.countByNameAndTemplateAndNotId(id, name, templateId);
        if (count > 0) {
            String message = "修改属性失败，属性名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeUpdateDTO, attribute);
        log.debug("即将执行【修改属性】的业务，ID：{}，参数：{}", id, attributeUpdateDTO);
        int rows = attributeMapper.update(attribute);
        if (rows != 1) {
            String message = "修改属性失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public List<AttributeListItemVO> list() {
        log.debug("开始处理【查询属性列表】的业务，参数：无");
        return attributeMapper.list();
    }

    @Override
    public List<AttributeListItemVO> listByTemplate(Long templateId) {
        log.debug("开始处理【根据属性模板查询属性列表】的业务，参数：{}", templateId);
        return attributeMapper.listByTemplate(templateId);
    }

    @Override
    public AttributeStandardVO getStandardById(Long id) {
        log.debug("开始处理【查询属性详情】的业务，参数：{}", id);
        AttributeStandardVO attribute = attributeMapper.getStandardById(id);
        // 是：数据不存在，抛出异常
        if (attribute == null) {
            String message = "查询属性详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回查询结果：{}", attribute);
        return attribute;
    }

}
